package com.crm.miaohe.service;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.depoist.request.DepositDetailParams;
import com.crm.miaohe.controller.depoist.request.DepositDetailQueryParams;
import com.crm.miaohe.mapper.TblDepositDetailsMapper;
import com.crm.miaohe.model.TblDepositDetails;
import com.crm.miaohe.model.TblDepositDetailsExample;
import com.crm.miaohe.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class DepositDetailServiceImpl implements DepositDetailService {
	
	private TblDepositDetailsMapper depositDetailsMapper;

	
	public DepositDetailServiceImpl(TblDepositDetailsMapper depositDetailsMapper) {
		this.depositDetailsMapper = depositDetailsMapper;
	}
	
	@Override
	public void addDepositDetail(DepositDetailParams params) {
		
		TblDepositDetails depositDetails = new TblDepositDetails();
		BeanUtils.copyProperties(params, depositDetails);
		depositDetailsMapper.insert(depositDetails);
	}

	@Override
	public PageResponse<TblDepositDetails> getDepositDetails(DepositDetailQueryParams params) {
		TblDepositDetailsExample example = new TblDepositDetailsExample();
		TblDepositDetailsExample.Criteria criteria = example.createCriteria();
	    
		if (!ObjectUtils.isEmpty(params.getCustomerId())) {
            criteria.andCustomerIdEqualTo(params.getCustomerId());
        }
		
		if (!ObjectUtils.isEmpty(params.getCustomerName())) {
            criteria.andCustomerNameLike(StringUtils.quoteWith(params.getCustomerName(), "%"));
        }
		
        PageInfo<TblDepositDetails> pageInfo =
                PageHelper.startPage(params.getPageNo(), params.getPageSize(), "create_time desc")
                        .doSelectPageInfo(() -> depositDetailsMapper.selectByExample(example));
        
        return PageResponse.<TblDepositDetails>builder()
                .currentPage(pageInfo.getPageNum())
                .totalPage(pageInfo.getPages())
                .totalElement(pageInfo.getTotal())
                .records(pageInfo.getList())
                .build();
	}

}
